package cn.myframe.controller.sys;

import cn.myframe.controller.AbstractController;
import cn.myframe.controller.BaseController;
import cn.myframe.entity.sys.SysParameterEntity;
import cn.myframe.log.LogUtils;
import cn.myframe.redis.RedisService;
import cn.myframe.service.BaseService;
import cn.myframe.service.sys.SysParameterService;
import cn.myframe.task.quartz.QuartzTask;
import cn.myframe.utils.DateUtils;
import cn.myframe.utils.core.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 存储系统参数配置信息
 * 
 * @author ynz
 * @email ynz@myframe.cn
 * @date 2018-08-24 15:02:00
 */
@RestController
@RequestMapping("/sys/sysparameter")
public class SysParameterController  extends AbstractController implements BaseController<SysParameterEntity,BaseService<SysParameterEntity>>{
	@Autowired
	private SysParameterService sysParameterService;
	
	@Autowired
	private RedisService redisService;
	
	@Autowired
	private QuartzTask quartzTask;
	
	public static String SYS_PARAMETER_MARK = "syspara:";
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list",method = {RequestMethod.GET})
	@RequiresPermissions("sysparameter:list")
	public R list(@RequestParam Map<String, Object> params){
		return this.defaultList(params);
	}	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sysparameter:info")
	public R info(@PathVariable("id") Long id){
		return this.defaultInfo(id,"sysParameter");
	}
	
	/**
	 * 新增保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysparameter:save")
	public R save(@RequestBody SysParameterEntity sysParameter){
		Pattern pattern = Pattern.compile(sysParameter.getRegularExp());
		Matcher matcher = pattern.matcher(sysParameter.getParamValue());
		boolean val = matcher.matches();
		if(!val){
			return R.error("参数值校验失败!");
		}
		redisService.hmSet(SYS_PARAMETER_MARK, sysParameter.getParamKey(), sysParameter.getParamValue());
		redisService.convertAndSend("dealParameterCache", sysParameter.getParamKey());
		return this.defaultSave(sysParameter);
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysparameter:delete")
	public R delete(@RequestBody Long[] ids){
		
		return this.defaultDelete(ids);
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")	
	@RequiresPermissions("sysparameter:update")
	public R update(@RequestBody SysParameterEntity sysParameter){
		Pattern pattern = Pattern.compile(sysParameter.getRegularExp());
		Matcher matcher = pattern.matcher(sysParameter.getParamValue());
		boolean val = matcher.matches();
		if(!val){
			return R.error("参数值校验失败!");
		}
		if(sysParameter.getParamKey().equals("logLevel")){
			LogUtils.setAllLogLevel(sysParameter.getParamValue());
		}
		sysParameter.setUpdateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
		sysParameter.setUpdateBy(getUserId());
		redisService.hmSet(SYS_PARAMETER_MARK, sysParameter.getParamKey(), sysParameter.getParamValue());
		redisService.convertAndSend("dealParameterCache", sysParameter.getParamKey());
		quartzTask.freshQuartz(2, sysParameter);
		return this.defaultUpdate(sysParameter);
	}
	
   /**
    * 提交,审核
    */
	@RequestMapping("/audit")	
	@RequiresPermissions(value={"sysparameter:submit","sysparameter:auditpass","sysparameter:auditnopass"},logical=Logical.OR)
	public R audit(@RequestBody Long[] ids ,@RequestParam Integer status){
		for(Long id : ids){
    		SysParameterEntity parameter = sysParameterService.queryObject(id);
    		quartzTask.freshQuartz(status, parameter);
    	}
		return this.defaultAudit(ids, status);
	}
	
   /**
	* 检验字段值重复
	*/
    @RequestMapping("/checkExist")
    @ResponseBody
    public String checkExist(String id,String value,String columnName){
        return this.defaultCheckExist(id, value, columnName);
    }
    
    public SysParameterService getService(){
		return sysParameterService;
	}

	@RequestMapping("/wxSingleRpMoneyScope")
	@ResponseBody
	public R wxSingleRpMoneyScope() {
		String singleMin = sysParameterService.getValue("wx_single_rp_min_money");
		singleMin = singleMin == null ? "1" : singleMin;
		String singleMax = sysParameterService.getValue("wx_single_rp_max_money");
		singleMax = singleMax == null ? "200" : singleMax;
    	return R.ok().put("singleMin", singleMin).put("singleMax", singleMax);
	}
}
